<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="40%"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="form" label-width="150px" :rules="rules">
      <el-form-item label="描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="被使用数量" prop="count">
        <el-input v-model="form.count" />
      </el-form-item>
      <el-form-item label="逾期追溯年限" prop="delay_year">
        <el-input v-model="form.delay_year" />
      </el-form-item>
      <el-form-item label="最大逾期数" prop="delay_count_max">
        <el-input v-model="form.delay_count_max" />
      </el-form-item>
      <el-form-item label="可以容忍的最多逾期天数" prop="delay_days_max">
        <el-input v-model="form.delay_days_max" />
      </el-form-item>
      <el-form-item label="可以容忍的逾期总金额" prop="delay_money_sum">
        <el-input v-model="form.delay_money_sum" />
      </el-form-item>
      <el-form-item label="单笔最大逾期金额" prop="delay_money_max">
        <el-input v-model="form.delay_money_max" />
      </el-form-item>
      <el-form-item label="最低年龄" prop="age_min">
        <el-input v-model="form.age_min" />
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch v-model="form.is_use" />
      </el-form-item>
      <el-form-item label="是否是默认引擎">
        <el-switch v-model="form.is_default" />
      </el-form-item>
      <el-form-item label="是否需要判断贷款">
        <el-switch v-model="form.delay_need" />
      </el-form-item>
      <el-form-item label="是否需要判断工作">
        <el-switch v-model="form.work_need" />
      </el-form-item>
      <el-form-item label="是否需要判断失信被执行人">
        <el-switch v-model="form.honest_need" />
      </el-form-item>
      <el-form-item label="是否需要判断年龄">
        <el-switch v-model="form.age_need" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch } from 'vue'
import { addRiskengine, updateRiskengine } from '@/api/riskengine'
import i18n from '@/i18n'
import { ElMessage } from 'element-plus'

const props = defineProps({
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  dialogTableValue: {
    type: Object,
    default: () => {}
  }
})

const formRef = ref(null)

const form = ref({
  description: '',
  is_use: true,
  count: 0,
  is_default: true,
  delay_need: true,
  work_need: true,
  honest_need: true,
  age_need: true,
  delay_year: 0,
  delay_count_max: 0,
  delay_days_max: 0,
  delay_money_sum: 0,
  delay_money_max: 0,
  age_min: 0
})

const rules = ref({
  description: [{ required: true, message: '请输入描述', trigger: 'blur' }],
  count: [{ required: true, message: '请输入被使用数量', trigger: 'blur' }],
  delay_year: [
    { required: true, message: '请输入逾期追溯年限', trigger: 'blur' }
  ],
  delay_count_max: [
    { required: true, message: '请输入最大逾期数', trigger: 'blur' }
  ],
  delay_days_max: [
    { required: true, message: '请输入可以容忍的最多逾期天数', trigger: 'blur' }
  ],
  delay_money_sum: [
    { required: true, message: '请输入可以容忍的逾期总金额', trigger: 'blur' }
  ],
  delay_money_max: [
    { required: true, message: '请输入单笔最大逾期金额', trigger: 'blur' }
  ],
  age_min: [{ required: true, message: '请输入最低年龄', trigger: 'blur' }]
})

watch(
  () => props.dialogTableValue,
  () => {
    form.value = props.dialogTableValue
  },
  { deep: true, immediate: true }
)

const emits = defineEmits(['update:modelValue', 'initEngineList'])
const handleClose = () => {
  emits('update:modelValue', false)
}
const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      form.value.count = parseInt(form.value.count)
      form.value.delay_year = parseInt(form.value.delay_year)
      form.value.delay_days_max = parseInt(form.value.delay_days_max)
      form.value.delay_money_sum = parseInt(form.value.delay_money_sum)
      form.value.delay_money_max = parseInt(form.value.delay_money_max)
      form.value.delay_count_max = parseInt(form.value.delay_count_max)
      form.value.age_min = parseInt(form.value.age_min)
      props.dialogTitle === '添加引擎'
        ? await addRiskengine(form.value)
        : await updateRiskengine(form.value)
      ElMessage({
        message: i18n.global.t('message.updateSuccess'),
        type: 'success'
      })
      emits('initEngineList')
      handleClose()
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
</script>

<style lang="scss" scoped></style>
